<?php 
Class indexController extends appController
{

	public function main()
	{
	
	   $favor=Model('index')->getFavor();
	   $this->assign('favor',$favor);
		//$this->captcha();
		$this->display('main.html');
	
	}
	public function uploadimg()
	{
		$cache_dir=UPLOAD_PATH.'/';
		$folder = @opendir($cache_dir);
		$dir=array();
		while ($file = readdir($folder))
		{
			if ($file == '.' || $file == '..' || $file == 'index.htm' || $file == 'index.html')
			{
				continue;
			}
			if (is_dir($cache_dir . $file))
			{
		
				$dir[]='/'.$file;
			}
		}
		$data=array();
		$this->assign('dir',$dir);
		//$this->captcha();
		$this->display('uploadimg.html');
	
	}
	public function act_upload()
	{
		
	    $path=$_POST['filepath'];
		$file=current($_FILES);
		$result=array('error'=>0,'url'=>$file['name']);
		$thumb_url=Model('image')->upload_image($file, $path.date('Ym'));
		$result['url']=$thumb_url;
		
		$result['message']='';
		print_r(json_encode($result));
		exit();
	}
	public function addfavor()
	{
		$info['name']=isset($_POST['name'])?strip_tags($_POST['name']):'';
		$info['data_id']=isset($_POST['data_id'])?$_POST['data_id']:'';
		$info['url']=isset($_POST['url'])?$_POST['url']:'';
		$result=array('error'=>1,'content'=>'已经收藏过了');
		if(Model('index')->is_only($info['data_id'],array('table'=>'favor','field'=>'data_id')))
		{
			$res=Model('index')->insert($info,'favor');
			if($res)
			{
				$result['error']=0;
				$result['content']='收藏成功';
			}
		}
		
		print_r(json_encode($result));
		exit();
	
	}
	public function delfavor()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:0;
		$result=array('error'=>0,'content'=>'删除成功');
		$res=Model('index')->del(array('id'=>$id,'table'=>'favor'));
		if(!$res)
		{
			$result=array('error'=>1,'content'=>'删除失败');
		}
		print_r(json_encode($result));
	}
	public function index()
	{
		
		//$this->captcha();
		$this->display('index.html');
		
	}
     public function clear_file()
	{
		$result=array('error'=>0,'content'=>'清除成功');
		//清除smarty缓存文件
		$cache_dir=ROOT_PATH. '/Temp/Cache/';
		$folder = @opendir($cache_dir);
		
		if ($folder === false)
		{
			return false;
		}
		
		while ($file = readdir($folder))
		{
			if ($file == '.' || $file == '..' || $file == 'index.htm' || $file == 'index.html')
			{
				continue;
			}
			if (is_file($cache_dir . $file))
			{
				
				@unlink($cache_dir . $file);
			}
		}
		
		//清除smarty编译文件
		$cache_dir=ROOT_PATH. '/Temp/templates_c/';
		$folder = @opendir($cache_dir);
		
		if ($folder === false)
		{
			return false;
		}
		
		while ($file = readdir($folder))
		{
			if ($file == '.' || $file == '..' )
			{
				continue;
			}
			if (is_file($cache_dir . $file))
			{
		
				@unlink($cache_dir . $file);
			}
		}
		
		$this->showMsg($result);
		
	}
	public function login()
	{
		
		
		if($_COOKIE["QIAQIA"][admin_id]||$_COOKIE["QIAQIA"][admin_pass])
		{
		//	cookie('QIAQIA[admin_id]',   '');
		//	cookie('QIAQIA[admin_pass]', '');
				
				
		//	session('[destroy]');
			
		}
		
		
		$this->display('login.html');
	}

	public function act_login()
	{
		$username=isset($_POST['username'])?hy_check($_POST['username'],'un',15):'';
		$password=isset($_POST['password'])?hy_check($_POST['password'],'',20):'';
		
		$captcha=isset($_POST['captcha'])?$_POST['captcha']:'';
		//print_r($captcha);
		$check_vali=$this->validator($captcha);
		
		if(!$check_vali||$captcha=='')
		{
			$this->showMsg('验证码错误','index.php?g=admin&m=index&a=login');
		}
		if($username=='#@_error'||$password=='#@_error')
		{
			Error::log('[后台登录错误]'.$_POST['username'].'--'.hy_getIP(),ADMIN_LOG);
			$this->showMsg(Lang('账号或者密码错误'),'/qq_admin.php');
		}
		if(Model('index')->check_user($username,$password))
		{
			header("Location:index.php?g=admin&m=index&a=index");
		}
		else 
		{
			Error::log('[后台登录错误]'.$_POST['username'].'--'.hy_getIP(),ADMIN_LOG);
			$this->showMsg(Lang('账号或者密码错误'),'index.php?g=admin&m=index&a=login');
			//header("Location:index.php?g=admin&m=index&a=login");
		}
		
	}
	
}